package com.example.cameratest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatTextView;


public class SlideTextView extends AppCompatTextView implements Runnable {
    private int currentScrollX;
    private boolean isStop = false;
    private int textWidth;
    String measureStr;
    private boolean isMeasure = false;
    private String tag = getClass().getSimpleName();

    public SlideTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public SlideTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlideTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
//        super.onDraw(canvas);

        if (!isMeasure || textChange()) {
            getTextWidth();
            isMeasure = true;
        }
        TextPaint paint = getPaint();
        int measuredHeight = getMeasuredHeight();
        int extendedPaddingBottom = (int) ((measuredHeight - getTextSize())/2);

        int y = measuredHeight - extendedPaddingBottom;
        int measuredWidth = getMeasuredWidth();

        canvas.drawText(measureStr, startx, y, paint);

        int i = startx + textWidth - measuredWidth;

        if (measuredWidth > textWidth) {
            if (startx + textWidth > measuredWidth) {
                canvas.drawText(measureStr, i - textWidth, y, paint);
            }
        } else {
            if (startx + textWidth > measuredWidth && startx >sp) {
                i = startx - sp;
                canvas.drawText(measureStr, i - textWidth, y, paint);
            }

        }
    }

    int sp = 100;

    private boolean textChange() {
        String string = getText().toString();
        if (measureStr == null && string == null) {
            return false;
        }
        return !(measureStr == null ? string.equals(null) : measureStr.equals(string));
    }

    int startx = 0;

    private void getTextWidth() {
        Paint paint = this.getPaint();
        measureStr = this.getText().toString();
        textWidth = (int) paint.measureText(measureStr);
        int measuredWidth = getMeasuredWidth();
        startx = measuredWidth >= textWidth ? getPaddingLeft() : measuredWidth - textWidth;
        Log.e(tag, " textWidth:" + textWidth + " measuredWidth:" + measuredWidth);
    }

    @Override
    public void run() {
        startx += 5;
        if (isStop) {
            return;
        }
        if (startx > getMeasuredWidth()) {
            int measuredWidth = getMeasuredWidth();
            if (measuredWidth > textWidth) {
                startx = 0;
            } else {
                startx = measuredWidth - (textWidth+sp);
            }
        }
        postDelayed(this, 50);
        postInvalidate();
    }


    public void startScroll() {
        isStop = false;
        this.removeCallbacks(this);
        post(this);
    }


    public void stopScroll() {
        isStop = true;
    }


    public void startFor0() {
        startx = getMeasuredWidth() - textWidth;
        startScroll();
    }
}